home *** CD-ROM | disk | FTP | other *** search
/ Oxygen Multimedia Graphics 22 / Oxygen Multimedia Graphics 22.iso / pc / System / OX22 / Internal_50_Random Movement and Rotation.ls < prev    next >
Encoding:
Text File  |  2008-03-12  |  8.4 KB  |  220 lines

  1. property pSprite, pCenterOffset, pLimits, pLimitsOrigin, pPath, pMovePeriod, pMoveStart, pMoveEnd, pRotate, pRotateStart, pRotatePeriod, pRotateEnd, pLimitsLeft, pLimitsRight, pLimitsTop, pLimitsBottom, pSpeed, pRotationSpeed, pLoopiness, pWackiness, perror
  2.  
  3. on getBehaviorDescription me
  4.   return "RANDOM MOVEMENT AND ROTATION" & RETURN & RETURN & "Sprite will move randomly within defined area, spinning wildly if so desired. " & "Author can control both the speed of the movement and speed of rotation, as well as how radically the movement and rotation vary. " & "Default movement limit is the stage area." & RETURN & RETURN & "PERMITTED MEMBER TYPES:" & RETURN & "animated GIF, bitmap, field, Flash, picture, text, vector shape" & RETURN & RETURN & "PARAMETERS:" & RETURN & "* Limit of movement (left, top, right, bottom)" & RETURN & "* Speed in pixels/second" & RETURN & "* Loopiness (how close the sprite's path is to a straight line)" & RETURN & "* Rotation speed (how fast the sprite rotates, with 1000 = 360 degrees/second)" & RETURN & "* Wackiness (how much the sprite's rotation may vary before changing direction)"
  5. end
  6.  
  7. on getBehaviorTooltip me
  8.   return "Animates a sprite along a random path and determines its motion and speed while it is animating."
  9. end
  10.  
  11. on beginSprite me
  12.   mInitialize(me)
  13. end
  14.  
  15. on prepareFrame me
  16.   if not perror then
  17.     mUpdate(me)
  18.   end if
  19. end
  20.  
  21. on mInitialize me
  22.   pSprite = sprite(me.spriteNum)
  23.   vMember = pSprite.member
  24.   perror = 0
  25.   if pRotationSpeed > 0 then
  26.     case vMember.type of
  27.       #field, #picture:
  28.         pRotationSpeed = 0
  29.         message = substituteStrings(me, "Sprite ^0: ^1 behavior cannot rotate #field or #picture sprites. " & "Set the Rotation Speed parameter for this sprite to 0 to prevent this dialog from appearing.", ["^0": pSprite.spriteNum, "^1": "Random Movement and Rotation"])
  30.         alert(message)
  31.         perror = 1
  32.     end case
  33.   end if
  34.   vRect = pSprite.rect
  35.   vHalfHeight = vRect.height / 2
  36.   vHalfWidth = vRect.width / 2
  37.   vMaxDimension = max(vHalfHeight, vHalfWidth)
  38.   vFarCorner = max(mVectorLength(pSprite.loc - point(vRect.left, vRect.top)), mVectorLength(pSprite.loc - point(vRect.right, vRect.top)), mVectorLength(pSprite.loc - point(vRect.left, vRect.bottom)), mVectorLength(pSprite.loc - point(vRect.right, vRect.bottom)))
  39.   vCenter = point(vHalfWidth, vHalfHeight) + point(vRect.left, vRect.top)
  40.   pCenterOffset = vCenter - pSprite.loc
  41.   pLimits = rect(pLimitsLeft, pLimitsTop, pLimitsRight, pLimitsBottom)
  42.   pLimits = pLimits + rect(vFarCorner, vFarCorner, -vFarCorner, -vFarCorner)
  43.   if (pLimits.width < vRect.width) or (pLimits.height < vRect.height) then
  44.     message = substituteStrings(me, "Sprite ^0: ^1 behavior movement limitations are too small to allow for full rotation of sprite.", ["^0": pSprite.spriteNum, "^1": "Random Movement and Rotation"])
  45.     alert(message)
  46.     perror = 1
  47.   end if
  48.   pLimitsOrigin = point(pLimits.left, pLimits.top)
  49.   if not perror then
  50.     mNewPath(me)
  51.     mNewRotation(me)
  52.   end if
  53. end
  54.  
  55. on substituteStrings me, parentString, childStringList
  56.   i = childStringList.count()
  57.   repeat while i
  58.     tempString = EMPTY
  59.     dummyString = childStringList.getPropAt(i)
  60.     replacement = childStringList[i]
  61.     lengthAdjust = dummyString.char.count - 1
  62.     repeat while 1
  63.       position = offset(dummyString, parentString)
  64.       if not position then
  65.         parentString = tempString & parentString
  66.         exit repeat
  67.         next repeat
  68.       end if
  69.       if position <> 1 then
  70.         tempString = tempString & parentString.char[1..position - 1]
  71.       end if
  72.       tempString = tempString & replacement
  73.       delete parentString.char[1..position + lengthAdjust]
  74.     end repeat
  75.     i = i - 1
  76.   end repeat
  77.   return parentString
  78. end
  79.  
  80. on mUpdate me
  81.   vTime = the milliSeconds
  82.   mMove(me, vTime)
  83.   mRotate(me, vTime)
  84. end
  85.  
  86. on mMove me, vTime
  87.   if pSpeed then
  88.     if vTime < pMoveEnd then
  89.       vElapsed = vTime - pMoveStart
  90.       if vElapsed > 0 then
  91.         vT1 = float(vElapsed) / pMovePeriod
  92.         vT2 = vT1 * vT1
  93.         vT3 = vT2 * vT1
  94.         vNewPosition = pPath.p0
  95.         vModPoint = pPath.dc * vT1
  96.         vNewPosition = vNewPosition + vModPoint
  97.         vModPoint = pPath.db * vT2
  98.         vNewPosition = vNewPosition + vModPoint
  99.         vModPoint = pPath.da * vT3
  100.         vNewPosition = vNewPosition + vModPoint
  101.         pSprite.loc = vNewPosition
  102.       end if
  103.     else
  104.       pSprite.loc = pPath.p3
  105.       mNewPath(me)
  106.     end if
  107.   end if
  108. end
  109.  
  110. on mRotate me, vTime
  111.   if pRotationSpeed then
  112.     if vTime < pRotateEnd then
  113.       vElapsed = vTime - pRotateStart
  114.       if vElapsed > 0 then
  115.         vRotation = pRotate.start + (pRotate.diff * vElapsed / pRotatePeriod)
  116.         pSprite.rotation = vRotation
  117.       end if
  118.     else
  119.       pSprite.rotation = pRotate.end
  120.       mNewRotation(me)
  121.     end if
  122.   end if
  123. end
  124.  
  125. on mNewPath me
  126.   if voidp(pPath) then
  127.     pPath = [#p0: pSprite.loc, #p1: pSprite.loc, #p2: pSprite.loc, #p3: pSprite.loc]
  128.   end if
  129.   if pSpeed then
  130.     vDest = point(random(pLimits.width), random(pLimits.height)) + pLimitsOrigin
  131.     vP0 = pPath.p3
  132.     vVector = vDest - vP0
  133.     vVectorLen = mVectorLength(vVector)
  134.     vLoopiness = vVectorLen * pLoopiness / 25
  135.     vP1 = mRestrain(vP0 + (pPath.p3 - pPath.p2), pLimits)
  136.     if vLoopiness then
  137.       vRandomPoint = point(mRandomSign() * random(vLoopiness), mRandomSign() * random(vLoopiness))
  138.     else
  139.       vRandomPoint = point(0, 0)
  140.     end if
  141.     vP2 = mRestrain(vP0 + (vVector * 2 / 3) + vRandomPoint, pLimits)
  142.     pPath = [#p0: vP0, #p1: vP1, #p2: vP2, #p3: vDest]
  143.     setaProp(pPath, #dc, 3 * (pPath.p1 - pPath.p0))
  144.     setaProp(pPath, #db, (3 * (pPath.p2 - pPath.p1)) - pPath.dc)
  145.     setaProp(pPath, #da, pPath.p3 - pPath.p0 - pPath.dc - pPath.db)
  146.     vDistance = mVectorLength(pPath.p0 - pPath.p1) + mVectorLength(pPath.p1 - pPath.p2) + mVectorLength(pPath.p2 - pPath.p3)
  147.     pMovePeriod = vDistance * 1000 / pSpeed
  148.     pMoveStart = the milliSeconds
  149.     pMoveEnd = pMoveStart + pMovePeriod
  150.   end if
  151. end
  152.  
  153. on mNewRotation me
  154.   if voidp(pRotate) then
  155.     pRotate = [#start: 0, #end: 0, #diff: 0]
  156.   end if
  157.   if pRotationSpeed then
  158.     vRotation = pSprite.rotation
  159.     if pRotate.diff < 0 then
  160.       vOffset = random(pWackiness)
  161.     else
  162.       vOffset = -random(pWackiness)
  163.     end if
  164.     vTargetRotation = vRotation + vOffset
  165.     pRotateStart = the milliSeconds
  166.     pRotatePeriod = abs(vOffset) * 1000 / pRotationSpeed * 1000 / 360
  167.     pRotateEnd = pRotateStart + pRotatePeriod
  168.     pRotate = [#start: vRotation, #end: vTargetRotation, #diff: vOffset]
  169.   end if
  170. end
  171.  
  172. on mVectorLength vVector
  173.   vSquare = (vVector.locH * vVector.locH) + (vVector.locV * vVector.locV)
  174.   return sqrt(vSquare)
  175. end
  176.  
  177. on mRandomSign
  178.   return (random(2) * 2) - 3
  179. end
  180.  
  181. on mRestrain vPoint, vRect
  182.   vPoint.locH = max(vRect.left, min(vRect.right, vPoint.locH))
  183.   vPoint.locV = max(vRect.top, min(vRect.bottom, vPoint.locV))
  184.   return vPoint
  185. end
  186.  
  187. on isOKToAttach me, aSpriteType, aSpriteNum
  188.   case aSpriteType of
  189.     #graphic:
  190.       return getPos([#animGif, #bitmap, #field, #flash, #picture, #text, #vectorShape], sprite(aSpriteNum).member.type) <> 0
  191.     #script:
  192.       return 0
  193.   end case
  194. end
  195.  
  196. on getPropertyDescriptionList me
  197.   if not (the currentSpriteNum) then
  198.     exit
  199.   end if
  200.   vRect = (the stage).rect
  201.   vRect = offset(vRect, -vRect.left, -vRect.top)
  202.   vMemberType = sprite(the currentSpriteNum).member.type
  203.   case vMemberType of
  204.     #text, #picture:
  205.       vRotateSpeed = 0
  206.     otherwise:
  207.       vRotateSpeed = 100
  208.   end case
  209.   vPDList = [:]
  210.   setaProp(vPDList, #pLimitsLeft, [#comment: "Limit of movement (left)", #format: #integer, #default: 0, #range: [#min: 0, #max: vRect.width]])
  211.   setaProp(vPDList, #pLimitsTop, [#comment: "Limit of movement (top)", #format: #integer, #default: 0, #range: [#min: 0, #max: vRect.height]])
  212.   setaProp(vPDList, #pLimitsRight, [#comment: "Limit of movement (right)", #format: #integer, #default: vRect.width, #range: [#min: 0, #max: vRect.width]])
  213.   setaProp(vPDList, #pLimitsBottom, [#comment: "Limit of movement (bottom)", #format: #integer, #default: vRect.height, #range: [#min: 0, #max: (the stage).rect.height]])
  214.   setaProp(vPDList, #pSpeed, [#comment: "Speed of movement", #format: #integer, #default: 100, #range: [#min: 0, #max: 1000]])
  215.   setaProp(vPDList, #pLoopiness, [#comment: "Loopiness", #format: #integer, #default: 10, #range: [#min: 0, #max: 25]])
  216.   setaProp(vPDList, #pRotationSpeed, [#comment: "Speed of rotation", #format: #integer, #default: vRotateSpeed, #range: [#min: 0, #max: 1000]])
  217.   setaProp(vPDList, #pWackiness, [#comment: "Wackiness", #format: #integer, #default: 120, #range: [#min: 0, #max: 360]])
  218.   return vPDList
  219. end
  220.